package com.whut.mobilesafe.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.whut.mobilesafe.R;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/22 19:32
 * 版    本：1.0
 * 描    述：组合控件
 */
public class SettingItemView extends RelativeLayout {

    private TextView tvTitle;
    private TextView tvDesc;
    private CheckBox cbChecked;
    private String mDescOn;
    private String mDescOff;

    public SettingItemView(Context context) {
        super(context);
        initView();

    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
        String title = a.getString(R.styleable.SettingItemView_my_title);
        mDescOn = a.getString(R.styleable.SettingItemView_desc_on);
        mDescOff = a.getString(R.styleable.SettingItemView_desc_off);
        if(cbChecked.isChecked()){
            tvDesc.setText(mDescOn);
        }else{
            tvDesc.setText(mDescOff);
        }
        setTitle(title);
    }

    /**
     * 当调用了有属性的构造方法之后 ，注意其他两个是给系统调用的
     * 这个就是你在xml中配置的属性
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        View view = View.inflate(getContext(), R.layout.setting_item_view, null);

        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvDesc = (TextView) view.findViewById(R.id.tv_desc);
        cbChecked = (CheckBox) view.findViewById(R.id.cb_check);

        this.addView(view);
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public boolean isChecked() {
        return cbChecked.isChecked();
    }

    //当组合控件被点击的时候，就调这一个方法就够了
    public void setChecked(boolean isChecked) {
        cbChecked.setChecked(isChecked);
        if (isChecked()) {
            tvDesc.setText(mDescOn);
        } else {
            tvDesc.setText(mDescOff);
        }

    }

}
